new Vue({
    el: '#app',
    mixins: [Query],//组件配置文件，路径/utils/components.js
    data() {
        return {
            form: {//查询筛选
                value: '',
                PageNumber: 1, //页码
                RowAmount: 20,
                Condition: utf8ToBase64("%%"),
            },
        }
    },
    mounted() {
        /**
         * 组件配置文件中自动执行方法
         * 1.this.getColumn(); //查询表头数据
         * 2.this.getList(this.form); //查询表格数据
         * 3.this.getTreeData();//查询树
         */
    },
    methods: {
        handleSerach() { //筛选
            this.tableData = this.Recursion(this.treeData, 'Crop_Type_Name', this.form.value);
            this.Total = this.tableData.length;
        },
        Recursion(arr, valueName, value) {//前端筛选树状节点
            const result = [];
            for (let i = 0; i < arr.length; i++) {
                const item = arr[i];
                const children = item.children || [];
                // 递归处理子节点
                const filteredChildren = this.Recursion(children, valueName, value);
                // 如果当前节点名称匹配搜索条件，或者有匹配的子节点，则保留
                const nameMatch = item[valueName].includes(value);
                const hasChildren = filteredChildren.length > 0;
                if (nameMatch || hasChildren) {
                    // 创建一个新对象，避免修改原数据
                    const newNode = { ...item };
                    newNode.children = filteredChildren;
                    result.push(newNode);
                }
            }
            return result;
        },
        getList() {
            httpRequest({
                url: '/generalBusiness/generalQueryForObtainingTrees',
                data: {
                    TableName: 'crop_type',
                    NodeCode: ''
                },
                type: "GET"
            }).then(data => {
                this.Total = data.length;//总数量
                this.tableData = data;
                this.treeData = data;
            }).catch(xhr => {
                this.$message({
                    message: '系统错误，请联系开发者!',
                    type: 'error'
                })
            })
        },
        handleChildAdd(row) {
            try {
                const { primaryKey } = $('#table').data('options');
                window.location.href = "./add.html?action=Add&nodeKey=" + row[primaryKey];
            } catch (error) {
                console.error(error);
                this.$message({
                    message: '系统错误，请联系开发者!',
                    type: 'error'
                })
            }
        }
    }
})